package net.kong.sns.rest.client.method;

import net.kong.sns.rest.client.ErrorResponse;
import net.kong.sns.rest.client.ApiError;
import net.kong.sns.rest.client.KongApiMethod;
import net.kong.sns.rest.client.rest.HttpResponseContent;

/**
 * @author <a href="mailto:lanhuai@gmail.com">Ning Yubin</a>
 * @version $Id: MethodInvokeBase.java,v 1.3 2009/11/04 17:17:38 ningyubin Exp $
 */
public abstract class MethodInvokeBase<T> extends AbstractMethodInvoke {

    protected MethodInvokeBase(KongApiMethod method, ResponseFormat responseFormat, String sessionKey) {
        super(method, responseFormat, sessionKey);
    }

    protected MethodInvokeBase(KongApiMethod method, ResponseFormat responseFormat) {
        super(method, responseFormat);
    }

    public abstract T parseResponse(HttpResponseContent responseData) throws ApiError;

    public abstract boolean isErrorResponse(HttpResponseContent responseData);

    public abstract ErrorResponse processErrorResponse(HttpResponseContent responseData);
    
    protected static String delimit( Iterable<?> iterable ) {
        return delimit(iterable, ",");
    }
    
    protected static String delimit( Iterable<?> iterable,String split) {
        if ( iterable == null ) {
            return null;
        }
        StringBuilder buffer = new StringBuilder();
        boolean empty = true;
        boolean notFirst = false;
        for ( Object item : iterable ) {
            if ( notFirst ) {
                buffer.append( split );
            } else {
                empty = false;
                notFirst = true;
            }
            buffer.append( item.toString() );
        }
        if ( empty ) {
            return null;
        }
        return buffer.toString();
    }
    
}
